namespace Sirius.Mapper.Internals
{
    using System;

    internal class PropertyMapper<T, TResult> : IPropertyMapper<T>, IEquatable<PropertyMapper<T, TResult>>
    {
        private readonly Func<T, TResult> getter;
        private readonly Action<T, TResult> setter;
        private readonly Type typeResult = typeof(TResult);
        private readonly string name;

        public PropertyMapper(string column, Func<T, TResult> getter, Action<T, TResult> setter)
        {
            this.getter = getter;
            this.setter = setter;
            this.name = column;
        }

        public string Name
        {
            get { return name; }
        }

        public Type PropertyType
        {
            get { return typeResult; }
        }

        public void Setter(T item, object value)
        {
            setter(item, (TResult) value);
        }

        public object Getter(T item)
        {
            object obj = getter(item);
            return obj;
        }

        public bool Equals(PropertyMapper<T, TResult> other)
        {
            return name == other.name;
        }

        public override int GetHashCode()
        {
            return name.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            if (obj is PropertyMapper<T, TResult>)
            {
                return Equals(obj as PropertyMapper<T, TResult>);
            }
            return false;
        }
    }
}